﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Framework.Message.SMS.SGIP.Commands
{
    /// <summary>
    /// 在SP和SMG的通信中，SMG用Deliver命令向SP发送一条MO短消息。SP接收到Deliver命令，会返回Deliver_Resp应答。SMG根据Deliver命令中目的特服号，判断出该服务属于和哪一个SMG相连接的SP，如果属于本地SP，则直接发送到SP，否则路由至相应的SMG。
    /// 在SMG和SMG的通信中，Deliver命令用于SMG客户端向服务器端路由MO短消息。服务器端接收到Deliver命令后，再发送到与之相连的目的SP。
    /// </summary>
    public class DeliverCommand : Command
    {
        #region 变量与属性
        private string _UserNumber = "";
        /// <summary>
        /// 发送短消息的用户手机号，手机号码前加“86”国别标志
        /// </summary>
        public string UserNumber
        {
            get { return _UserNumber; }
            set { _UserNumber = value; }
        }

        private string _SPNumber = "";
        /// <summary>
        /// SP的接入号码
        /// </summary>
        public string SPNumber
        {
            get { return _SPNumber; }
            set { _SPNumber = value; }
        }

        private int _TP_pid = 0;
        /// <summary>
        /// GSM协议类型。详细解释请参考GSM03.40中的9.2.3.9
        /// </summary>
        public int TP_pid
        {
            get { return _TP_pid; }
            set { _TP_pid = value; }
        }

        private int _TP_udhi = 0;
        /// <summary>
        /// GSM协议类型。详细解释请参考GSM03.40中的9.2.3.23,仅使用1位，右对齐
        /// </summary>
        public int TP_udhi
        {
            get { return _TP_udhi; }
            set { _TP_udhi = value; }
        }

        private MessageCoding _MessageCoding = MessageCoding.Gbk;
        /// <summary>
        /// 短消息的编码格式。
        /// </summary>
        public MessageCoding MessageCoding
        {
            get { return _MessageCoding; }
            set { _MessageCoding = value; }
        }

        private uint _MessageLength = 0;
        /// <summary>
        /// 短消息的长度
        /// </summary>
        public uint MessageLength
        {
            get { return _MessageLength; }
            set
            {
                if (_MessageLength != value)
                {
                    _MessageLength = value;
                    Header.MessageLength = CommandHeader.HeaderLength + this.BodyLength;
                }
            }
        }

        private string _MessageContent = "";
        /// <summary>
        /// 短消息的内容
        /// </summary>
        public string MessageContent
        {
            get { return _MessageContent; }
            set
            {
                if (_MessageContent != value)
                {
                    _MessageContent = value;
                    this.MessageLength = (uint)ByteStringConvert.GetString2ByteCount(_MessageContent, _MessageCoding);
                }
            }
        }

        private string _Reserve = "";
        /// <summary>
        /// 保留，扩展用
        /// </summary>
        public string Reserve
        {
            get { return _Reserve; }
            set { _Reserve = value; }
        }
        #endregion

        #region 构造函数
        public DeliverCommand()
        {

        }

        public DeliverCommand(byte[] bytes)
            : base(bytes)
        {
        }

        public DeliverCommand(byte[] bytes, int startIndex)
            : base(bytes, startIndex)
        {
        }

        public DeliverCommand(CommandSequence sequence)
            : base(sequence)
        {
        }
        #endregion

        public override uint BodyLength
        {
            get { return (uint)(21 + 21 + 1 + 1 + 1 + 4 + this.MessageLength + 8); }
        }

        public override SGIPCommandType CommandType
        {
            get { return SGIPCommandType.Deliver; }
        }

        protected override void BytesTo(byte[] bytes, int startIndex)
        {
            int i = (int)CommandHeader.HeaderLength;
            base.BytesTo(bytes, startIndex);
            _UserNumber = ByteStringConvert.Bytes2String(bytes, i, 21, MessageCoding.ASCII).TrimEnd('\0');
            i += 21;
            _SPNumber = ByteStringConvert.Bytes2String(bytes, i, 21, MessageCoding.ASCII).TrimEnd('\0');
            i += 21;
            _TP_pid = (int)bytes[i];
            i++;
            _TP_udhi = (int)bytes[i];
            i++;
            _MessageCoding = (MessageCoding)bytes[i];
            i++;
            _MessageLength = ByteIntConvert.Bytes2UInt(bytes, i);
            i += 4;
            _MessageContent = ByteStringConvert.Bytes2String(bytes, i, (int)_MessageLength, this._MessageCoding).TrimEnd('\0');
            i += (int)_MessageLength;
            _Reserve = ByteStringConvert.Bytes2String(bytes, i, 8, MessageCoding.ASCII).TrimEnd('\0');
        }

        public override byte[] ToBytes()
        {
            int i = (int)CommandHeader.HeaderLength;
            byte[] bytes = base.ToBytes();
            ByteStringConvert.String2Bytes(_UserNumber.PadRight(21, '\0'), MessageCoding.ASCII).CopyTo(bytes, i);
            i += 21;
            ByteStringConvert.String2Bytes(_SPNumber.PadRight(21, '\0'), MessageCoding.ASCII).CopyTo(bytes, i);
            i += 21;
            bytes[i] = (byte)this._TP_pid;
            i++;
            bytes[i] = (byte)this._TP_udhi;
            i++;
            bytes[i] = (byte)this._MessageCoding;
            i++;
            ByteIntConvert.UInt2Bytes(this.MessageLength).CopyTo(bytes, i);
            i += 4;
            ByteStringConvert.String2Bytes(this._MessageContent, this._MessageCoding).CopyTo(bytes, i);
            i += (int)_MessageLength;
            ByteStringConvert.String2Bytes(this._Reserve.PadRight(8, '\0'), MessageCoding.ASCII).CopyTo(bytes, i);
            return bytes;
        }
    }
}
